package org.huhuiyu.easyui.action;

import java.util.List;

import org.apache.log4j.Logger;
import org.huhuiyu.easyui.enity.EmployeeModel;
import org.huhuiyu.easyui.enity.PageCountBean;
import org.huhuiyu.easyui.service.EmployeeService;

import com.opensymphony.xwork2.Action;

public class EmployeeAction {
	private static Logger logger = Logger.getLogger(EmployeeAction.class);
	private EmployeeService employeeService;
	private EmployeeModel employeeModel;
	private String actionMessage;
	private int rows;
	private int page;
	private PageCountBean pageBean;

	public EmployeeAction() {
	}

	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	public EmployeeService getEmployeeService() {
		return employeeService;
	}

	public EmployeeModel getEmployeeModel() {
		return employeeModel;
	}

	public void setEmployeeModel(EmployeeModel employeeModel) {
		this.employeeModel = employeeModel;
	}

	public String getActionMessage() {
		return actionMessage;
	}

	public void setActionMessage(String actionMessage) {
		this.actionMessage = actionMessage;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String queryAll() throws Exception {
		int total = employeeService.queryEmployeeModelCount();
		pageBean = new PageCountBean(total, page, rows);
		List<EmployeeModel> list = employeeService.queryPageEmployeeModel(
				pageBean.getStart(), pageBean.getPageSize());
		logger.debug(list);
		JsonActionUtil util = JsonActionUtil.getSuccessJson("rows", list);
		util.putData("total", total);
		util.putData("pageBean", pageBean);
		util.write();
		return Action.NONE;
	}

	public String save() throws Exception {
		logger.debug(employeeModel);
		JsonActionUtil util = new JsonActionUtil();
		try {
			employeeService.saveEmployeeModel(employeeModel);
			util.putData("message", "添加成功！");
			util.setSuccess(true);
		} catch (Exception e) {
			util.putData("message", "添加失败！");
			util.setSuccess(false);
			logger.fatal("程序发生错误", e);
		}
		util.write();
		return Action.NONE;
	}

	public String delete() throws Exception {
		logger.debug(employeeModel);
		JsonActionUtil util = new JsonActionUtil();
		try {
			employeeModel = employeeService
					.queryEmployeeModelByKey(employeeModel);
			if (employeeModel != null) {
				employeeService.deleteEmployeeModel(employeeModel);
			}
			util.putData("message", "删除成功！");
			util.setSuccess(true);
		} catch (Exception e) {
			util.putData("message", "删除失败！");
			util.setSuccess(false);
			logger.fatal("程序发生错误", e);
		}
		util.write();
		return Action.NONE;
	}

	public String queryByKey() throws Exception {
		logger.debug(employeeModel);
		employeeModel = employeeService.queryEmployeeModelByKey(employeeModel);
		logger.debug(employeeModel);
		JsonActionUtil util = JsonActionUtil.getSuccessJson("data",
				employeeModel);
		util.write();
		return Action.NONE;
	}

	public String modify() throws Exception {
		logger.debug(employeeModel);
		JsonActionUtil util = new JsonActionUtil();
		try {
			employeeService.updateEmployeeModel(employeeModel);
			util.putData("message", "修改成功！");
			util.setSuccess(true);
		} catch (Exception e) {
			util.putData("message", "修改失败！");
			util.setSuccess(false);
			logger.fatal("程序发生错误", e);
		}
		util.write();
		return Action.NONE;
	}

}
